package com.wfcm.service.impl;

import com.wfcm.entity.WfCourseChapterEntity;
import com.wfcm.service.WfCardCourseRelationService;
import com.wfcm.service.WfCourseChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wfcm.dao.WfGoodsOrderRelationDao;
import com.wfcm.entity.WfGoodsOrderRelationEntity;
import com.wfcm.service.WfGoodsOrderRelationService;



@Service("wfGoodsOrderRelationService")
public class WfGoodsOrderRelationServiceImpl implements WfGoodsOrderRelationService {
	@Autowired
	private WfGoodsOrderRelationDao wfGoodsOrderRelationDao;
	@Autowired
	private WfCourseChapterService courseChapterService;
	@Autowired
	private WfCardCourseRelationService cardCourseRelationService;
	
	@Override
	public WfGoodsOrderRelationEntity queryObject(Integer id){
		return wfGoodsOrderRelationDao.queryObject(id);
	}
	
	@Override
	public List<WfGoodsOrderRelationEntity> queryList(Map<String, Object> map){
		return wfGoodsOrderRelationDao.queryList(map);
	}

	@Override
	public List<WfGoodsOrderRelationEntity> queryYDLJ(Map<String, Object> map){
		return wfGoodsOrderRelationDao.queryYDLJ(map);
	}

	@Override
	public int queryGoodOrNot(Map<String, Object> map){
		return wfGoodsOrderRelationDao.queryGoodOrNot(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return wfGoodsOrderRelationDao.queryTotal(map);
	}
	
	@Override
	public void save(WfGoodsOrderRelationEntity wfGoodsOrderRelation){
		wfGoodsOrderRelationDao.save(wfGoodsOrderRelation);
	}
	
	@Override
	public void update(WfGoodsOrderRelationEntity wfGoodsOrderRelation){
		wfGoodsOrderRelationDao.update(wfGoodsOrderRelation);
	}
	
	@Override
	public void delete(Integer id){
		wfGoodsOrderRelationDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		wfGoodsOrderRelationDao.deleteBatch(ids);
	}

	@Override
	public List<String> queryOrderCode(WfGoodsOrderRelationEntity goodsOrderRelation) {
		return wfGoodsOrderRelationDao.queryOrderCode(goodsOrderRelation);
	}

	@Override
	public Boolean confirm(WfGoodsOrderRelationEntity gor) {
		Boolean boo = true;
		Integer chapterId = null;
		Integer courseId = null;
		List<Integer> vipCardIds = new ArrayList<>();
		switch (gor.getGoodssort()) {
			case 1:
				courseId = gor.getGoodsid();
				vipCardIds = cardCourseRelationService.queryCardIdByCourseId(courseId);
				break;
			case 2:
				chapterId = gor.getGoodsid();
				courseId = courseChapterService.queryChapter(gor.getGoodsid()).getCourseid();
				vipCardIds = cardCourseRelationService.queryCardIdByCourseId(courseId);
				break;
			case 3:
				vipCardIds.add(gor.getGoodsid());
		}
		if (null != chapterId) {
			boo = wfGoodsOrderRelationDao.queryOrderCode(
					new WfGoodsOrderRelationEntity(gor.getUserid(), chapterId, 2)).size() == 0;
		}
		if (boo && null != courseId) {
			boo = wfGoodsOrderRelationDao.queryOrderCode(
					new WfGoodsOrderRelationEntity(gor.getUserid(), courseId, 1)).size() == 0;
		}
		if (boo && vipCardIds.size() > 0) {
			OUT:
			for (Integer vipCardId : vipCardIds) {
				boo = wfGoodsOrderRelationDao.queryOrderCode(
						new WfGoodsOrderRelationEntity(gor.getUserid(), vipCardId, 3)).size() == 0;
				if (!boo) {
					break OUT;
				}
			}
		}
		return boo;
	}

}
